package com.kedacom.freedomLabel.viewmodel.report;

import android.arch.lifecycle.MutableLiveData;
import android.text.TextUtils;

import com.kedacom.freedomLabel.AppExecutors;
import com.kedacom.freedomLabel.base.BaseViewModel;
import com.kedacom.freedomLabel.model.bean.Business;
import com.kedacom.freedomLabel.model.db.AppDatabase;
import com.kedacom.lego.mvvm.ObservableLiveDataField;

import java.util.List;

public class ReportDetailViewModel extends BaseViewModel {

    // 明细
    public MutableLiveData<List<Business>> businessList = new MutableLiveData<>();

    // 选中数量
    public ObservableLiveDataField<Integer> selectNum = new ObservableLiveDataField<>(0);

    public ObservableLiveDataField<String> deleteResult = new ObservableLiveDataField<>();

    public void delete(List<Integer> ids) {
        AppExecutors.getInstance().queryIO()
                .submit(() -> {
                    try {
                        AppDatabase.getInstance().businessDao()
                                .deleteByIds(ids);
                        deleteResult.set(null);
                    } catch (Exception e) {
                        e.printStackTrace();
                        deleteResult.set(e.getMessage());
                    }
                });
    }

    public void deleteAll(long startTime, long endTime, String productId, String cardNumber) {
        AppExecutors.getInstance().queryIO()
                .submit(() -> {
                    try {
                        if (TextUtils.isEmpty(productId)) {
                            if (TextUtils.isEmpty(cardNumber)) {
                                AppDatabase.getInstance().businessDao()
                                        .delete(startTime, endTime);
                            } else {
                                AppDatabase.getInstance().businessDao()
                                        .deleteCardNumber(cardNumber, startTime, endTime);
                            }
                        } else {
                            if (TextUtils.isEmpty(cardNumber)) {
                                AppDatabase.getInstance().businessDao()
                                        .deleteProduct(productId, startTime, endTime);
                            } else {
                                AppDatabase.getInstance().businessDao()
                                        .deleteProductAndCardNumber(productId, cardNumber, startTime, endTime);
                            }
                        }
                        deleteResult.set(null);
                    } catch (Exception e) {
                        e.printStackTrace();
                        deleteResult.set(e.getMessage());
                    }
                });
    }


    public void getSummaryDetail(long startTime, long endTime, String productId, String cardNumber) {
        AppExecutors.getInstance().queryIO()
                .submit(() -> {
                    try {
                        List<Business> businesses;
                        if (TextUtils.isEmpty(productId)) {
                            if (TextUtils.isEmpty(cardNumber)) {
                                businesses = AppDatabase.getInstance().businessDao()
                                        .getBusinessList(startTime, endTime);
                            } else {
                                businesses = AppDatabase.getInstance().businessDao()
                                        .getBusinessListWithOperator(cardNumber, startTime, endTime);
                            }
                        } else {
                            if (TextUtils.isEmpty(cardNumber)) {
                                businesses = AppDatabase.getInstance().businessDao()
                                        .getBusinessList(startTime, endTime, productId);
                            } else {
                                businesses = AppDatabase.getInstance().businessDao()
                                        .getBusinessListProductIdAndCardNumber(startTime, endTime, productId, cardNumber);
                            }
                        }
                        businessList.postValue(businesses);
                    } catch (Exception e) {
                        e.printStackTrace();
                        showToast(e.getMessage(), TOAST_ERROR);
                    }
                });
    }
}
